package de.dfki.asr.genesis2.sms.oauth;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import net.oauth.exception.OAuthException;
import net.oauth.signature.OAuthSignature;
import net.oauth.signature.impl.OAuthHmacSha1Signature;
import net.oauth.token.oauth1.AccessToken;
import net.oauth.token.oauth1.AuthorizedToken;
import net.oauth.token.oauth1.RequestToken;

import com.neurologic.oauth.service.impl.OAuth1Service;

/**
 * @author Buhake Sindi
 * @since 31 May 2011
 *
 */
public class TwitterOAuthService extends OAuthServlet {

    public static final String TWITTER_REQUEST_TOKEN_SESSION = "TWITTER_REQUEST_TOKEN_SESSION";
    public static final String TWITTER_ACCESS_TOKEN_SESSION = "TWITTER_ACCESS_TOKEN_SESSION";
    protected static final String redirect_uri = "http://localhost:8080/sms/TwitterOAuthService";
    protected static final String client_id = "ILyOZn3hJDue8cRePFH94w";
	protected static final String client_secret = "9zHE8QRaaIsJwJlSCLuJVik9hv6plgVZS45G2lg";
	protected static final String callback_url = "http://localhost:8080/Test/TestFacebookServlet";
	protected static final String accessToken_url = "https://graph.facebook.com/oauth/access_token?";
	protected static final String requestCode_url = "https://api.twitter.com/dialog/oauth/request_token?";

    /* (non-Javadoc)
     * @see com.neurologic.oauth.service.impl.OAuth1Service#getOAuthSignature()
     */
    
    public TwitterOAuthService() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String access_token = request.getParameter("access_token");
		String expires = request.getParameter("expires");
		String state = request.getParameter("state");
		String code = request.getParameter("code");
		
		System.out.println(state);
		System.out.println(code);
		
		String url = accessToken_url + 
					 "oauth_consumer_key=" + client_id +
					 "&oauth_nonce=" + client_secret +
					 "&oauth_callback=" + redirect_uri + 
					 "&code=" + code;
		
		System.out.println(url);			 
		
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);
		HttpResponse resp = client.execute(get);
		String body = EntityUtils.toString(resp.getEntity());
		String params[] = body.split("&");
		access_token = params[0].replace("access_token=", "");
		expires = params[1].replace("expires=", "");
		System.out.println(access_token);
		System.out.println(expires);
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}